/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import modelo.Contrato;
import util.JPAUtil;

@FacesConverter(forClass = Contrato.class)
public class ContratoConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String string) {
        Long id = null;
        try {
            if (string == null || string.isEmpty()) {
                return null;
            }
            id = Long.valueOf(string);
        } catch (NumberFormatException e) {
            return null;
        }

        Contrato estado = JPAUtil.getEntityManager().find(Contrato.class, id);

        return estado;


    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Contrato estado = (Contrato) value;
        if (estado == null || estado.getId() == null) {
            return null;
        }
        return String.valueOf(estado.getId());
    }
}
